﻿<!DOCTYPE html>
<html>
<head lang="en">

<span class="comments">/**</span><br />
				<span class="space"/><span class="comments">*扫雷游戏</span><br />
				<span class="space"/><span class="comments">* 1，右击蓝色代表标记坐标为雷.</span><br />
				<span class="space"/><span class="comments">* 2，绿色代表无雷区域</span><br />
		var data=new Array( 
 
<input type="button" value="开始扫雷游戏" onclick="initMine(10)"><input type="button" value="重置扫雷游戏" onclick="reset()">
<body oncontextmenu="return hidecontextmenu()">
     <script language="javascript">
	/*var data=new Array( 
[0,0,0,0,0,0,0,0,0,0,0], 
[0,0,0,0,0,0,0,0,0,0,0], 
[0,0,0,0,0,0,0,0,0,0,0], 
[0,0,0,0,0,0,0,0,0,0,0], 
[0,0,0,0,0,0,0,0,0,0,0], 
[0,0,0,0,0,0,0,0,0,0,0], 
[0,0,0,0,0,0,0,0,0,0,0], 
[0,0,0,0,0,0,0,0,0,0,0], 
[0,0,0,0,0,0,0,0,0,0,0], 
[0,0,0,0,0,0,0,0,0,0,0], 
[0,0,0,0,0,0,0,0,0,0,0] 
); */
	 //FF0000 红色
	 //999999 深灰色
	 //CCCCCC 浅灰色
	 //00FF00 绿色
	 var iterate=new Array();
	 var iterateCount=0;
	 var nodeId=0;
	 var bool_1=0;bool_2=0;bool_3=0;bool_4=0;
	 var mineNumber=10;
	 //鼠标从单元格移开
	 function moveOut(move)
    {
	  //alert("move out");
	  if(bool_3){
	      if(move.style.backgroundColor=="#999999"){
              move.style.backgroundColor="#CCCCCC";
	      }
	   }
    }
	 
	 //鼠标移上单元格
     function moveOver(move,i)
    {
	   if(bool_3){
	  //alert("move on");
	  //这里的操作意思是防止变成红色的单元格褪色,防止是雷的单元格未被点中之前鼠标移上去不变色
	     if(bool_1==0){
               move.style.backgroundColor="#999999";
	     }
	     if(bool_1==1){
	        if(document.all.mine[i].value==0){
		       move.style.backgroundColor="#999999";
		    }
	     }
	     if(document.all.mine[i].value==10){
	        move.style.backgroundColor="#00FF00";
	      }
		  if(document.all.temp[i].value%2==1){
		     move.style.backgroundColor="#00FFFF";
		  }
	   }
    }
	
	
	function hidecontextmenu()
    {
      return false;
    }
	
	
	
	function showAllMine(){
	   for(var i=0;i<100;i++){
	       if(document.all.mine[i].value==9){
		       document.getElementById("tdBar"+i).style.backgroundColor="#FF0000";
			}
	   }
	   alert("你输了!");
	   bool_3=0;
	}
	
	function showNum(move,i)
	{
	   if(bool_3){
	       if(document.all.mine[i].value==9)
	      {
	          bool_1=1;
	          move.style.backgroundColor="#FF0000";
		      showAllMine();
	      }else{
		      var x=i%10;
		      var y=i/10-x*0.1;
		      x=Math.round(x);
		      y=Math.round(y);
			  document.all.temp[i].value=2;
		      node(x,y);
	      }
	   }
	}
	
	function initMine(num)
	{
	    bool_3=1;
	    var i,randomnumber;
		
		for(i=num;i>0;i--){
		randomnumber=Math.floor(Math.random()*100);
		if(document.all.mine[randomnumber].value!=9){
		document.all.mine[randomnumber].value=9;
	    }
		else i++;
	   }
	}
	
	function position(x,y){
	  if(x<0||x>9||y<0||y>9)return false;
	  else return true;
	}
	
	//计算结点周围的雷的个数
	function getMineNum(x,y){
	   var num=0;
	   //检验当前结点左右上下八格是否有雷
	   for(var i=x-1;i<=x+1;i++){
	       if(i>=0&&i<=9){
		       for(var j=y-1;j<=y+1;j++){
			      if(j>=0&&j<=9){
				  //若有雷,当前结点标识雷数加1
				      if(document.all.mine[i+j*10].value==9){
				      num=num+1;
					  }
				   }
	           }
	       }
	   }
	   return num;
	}
	
	//得到结点雷数为0的周围结点
	function getNode(x,y){
	   
	   var hasMine=new Array();
	   var count=0;
	   var has=0;
	   if(iterate==null||iterate.length==0){
					  iterate[iterateCount++]=x+y*10;
					  }
					  //扫描结点周围八个结点
	   for(var i=x-1;i<=x+1;i++){
	       if(i>=0&&i<=9){
		      //alert("x坐标是:"+i);
		       for(var j=y-1;j<=y+1;j++){
			      if(j>=0&&j<=9){
				     //alert("y坐标是:"+j);
					 //去除结点本身
				     if((i+j*10)!=(x+y*10)){
				         for(var n=0;n<iterate.length;n++){
					     if(iterate[n]==i+j*10)
						 has++;
					  }
					  //去除重复结点
					  if(has==0){
						 //alert("放入的数字是:"+(i+j*10));
						 //将结点坐标放入数组
					     hasMine[count++]=i+j*10;
					    iterate[iterateCount++]=i+j*10;
					  }	
					  has=0;
					 }
	              }
			   }
		   }
	   }
	   return hasMine;
	}
	
	//结点处理
	function node(x,y){
	
	var hasMine=new Array();
	//若结点超出表格范围,返回上一级
	if(x<0||x>9||y<0||y>9)return; 
	//得到周围雷的个数            
	var mineNum=getMineNum(x,y);
	//雷的个数不为零,页面显示雷的个数,返回
	if(mineNum!=0){
	    document.all.mine[x+y*10].value=mineNum;
		document.getElementById(x+y*10).innerHTML=mineNum;
		document.all.temp[x+y*10].value=2;
		//雷的个数为零,执行以下操作
	}else{
	   var i=x+y*10;
	   //当前结点显示为0,背景为绿色
	   document.getElementById(i).innerHTML=0;
	   document.getElementById("tdBar"+i).style.backgroundColor="#00FF00";
	   document.all.mine[i].value=10;
	   document.all.temp[i].value=2;
	   //根据当前结点,得到周围其他结点
	   hasMine=getNode(x,y);
	   //如果周围有其它结点
	   if(hasMine.length!=0){
	   //alert(hasMine.length);
	   //从数组中拿出每个周围的结点,再次进行下一级结点处理
	   for(i=0;i<hasMine.length;i++){
          var n=hasMine[i]%10;
		  var m=hasMine[i]/10-n*0.1;
		  n=Math.round(n);
		  m=Math.round(m);
		  
	      node(n,m);
		  }//没有则返回
	    }else return;
	   }
	}
	
	function mouseRightDown(move,i){
	   var lastMine=0;
	   if(bool_3){ 
	     if(event.button==2&&(document.all.mine[i].value==0||document.all.mine[i].value==9)){
		     document.all.temp[i].value++;
		     if(document.all.temp[i].value%2==0){
		          move.style.backgroundColor="#999999";
				  if(document.all.mine[i].value==9){
				      mineNumber++;
				  }
			 }
			 else if(document.all.temp[i].value%2==1)
			 {
			      move.style.backgroundColor="#00FFFF";
				  if(document.all.mine[i].value==9){
				      mineNumber--;
				  }
			 }
			 for(var m=0;m<100;m++){
			     if(mineNumber!=0&&document.all.temp[m].value==0){
				     lastMine++;
			      }  
			 }
			 if(mineNumber==0&&lastMine==0){
			     alert("你赢啦!");
				 lastMine=0;
				 reset();
			 }
		 }
	   }
	}
	
	//重置页面
	function reset(){
	   for(var i=0;i<100;i++)
	   {
	       document.all.mine[i].value=0;
		   document.all.temp[i].value=0;
		   document.getElementById("tdBar"+i).style.backgroundColor="#CCCCCC";
		   document.getElementById(i).innerHTML="";
		   iterate.length=0;
		   iterateCount=0;
	       nodeId=0;
	       bool_1=0;
		   bool_2=0;
		   bool_3=0;
		   mineNumber=10;
	    }
    }
	
	document.writeln("<table width='400' height='400' border='1' align='center'>");
	for(var tr=0;tr<10;tr++){
	    document.writeln("<tr height='10%' bgcolor='#CCCCCC'>");
	       for(var td=0;td<10;td++){
		       var tdr=tr*10+td;
			   //alert(tdr);
		       document.writeln("<td width='10%' onmouseout='moveOut(this)' onmouseover='moveOver(this,"+tdr+")' onmousedown='mouseRightDown(this,"+tdr+")' onClick='showNum(this,"+tdr+")' id='tdBar"+tdr+"'>");
			   document.writeln("<input type='hidden' name='mine' value='0'><input type='hidden' name='temp' value='0'><div id='"+tdr+"' align='center'><\/div><\/td>");
		   }
	    document.writeln("<\/tr>");
	 }
	 document.writeln("<\/table>");
	 </script>
	 





</body>
</html>



</script>
</html>
